import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCdelete {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        //1.创建并初始化一个数据源
        DataSource dataSource = new MysqlDataSource();

        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/yjh1?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("yjh20010223");
        //2.连接数据库服务器
        Connection connection = dataSource.getConnection();
        //输入删除条件
        System.out.println("请输入你要删除学生的姓名");
        String name = scanner.next();
        //3.构造sql 语句
        String sql = "delete from student1 where name = ? ";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        int ret = statement.executeUpdate();
        System.out.println("ret ="+ ret);
        //5.释放必要的资源
        statement.close();
        connection.close();

    }
}
